package com.qin.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.qin.mall.domain.PmsProductAttribute;
import com.qin.mall.domain.PmsProductAttributeCategory;
import com.qin.mall.mapper.PmsProductAttributeCategoryMapper;
import com.qin.mall.mapper.PmsProductAttributeMapper;
import com.qin.mall.mapper.PmsProductCategoryAttributeRelationMapper;
import com.qin.mall.service.PmsProductAttributeService;
import com.qin.mall.domain.PmsProductCategoryAttributeRelation;
import com.qin.mall.dto.ProductAttributeParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 覃剑
* @description 针对表【pms_product_attribute(商品属性参数表)】的数据库操作Service实现
* @createDate 2022-11-06 09:20:06
*/
@Service
public class PmsProductAttributeServiceImpl extends ServiceImpl<PmsProductAttributeMapper, PmsProductAttribute>
    implements PmsProductAttributeService {
    
    @Autowired
    private PmsProductAttributeMapper productAttributeMapper;
    @Autowired
    private PmsProductAttributeCategoryMapper productAttributeCategoryMapper;
    
    @Autowired
    private PmsProductCategoryAttributeRelationMapper pmsProductCategoryAttributeRelationMapper;


    // TODO 根据分类分页获取商品属性
    // 传入商品分类id cid分类类型(数据表里面没有这个字段) 分页页数,每页大小
    // 实体那边修改后就有 cid 了
    @Override
    public Page<PmsProductAttribute> getList(Long cid, Integer type, Integer pageNum,
                                             Integer pageSize) {
        Page<PmsProductAttribute> page = new Page<>(pageNum, pageSize);
        QueryWrapper<PmsProductAttribute> wrapper = new QueryWrapper<>();
        wrapper.eq("type",type)
                .eq("product_attribute_category_id",cid);
        Page<PmsProductAttribute> pages = productAttributeMapper.selectPage(page, wrapper);
        return pages;
    }

    // TODO 添加商品属性
    @Override
    public int create(ProductAttributeParam productAttributeParam) {
        PmsProductAttribute pmsProductAttribute = new PmsProductAttribute();
        BeanUtils.copyProperties(productAttributeParam, pmsProductAttribute);
        
        int insert = productAttributeMapper.insert(pmsProductAttribute);
        //新增商品属性以后需要更新商品属性分类数量
        PmsProductAttributeCategory pmsProductAttributeCategory = productAttributeCategoryMapper.selectById(pmsProductAttribute.getCid());
        if(pmsProductAttribute.getType()==0){
            pmsProductAttributeCategory.setAttributeCount(pmsProductAttributeCategory.getAttributeCount()+1);
        }else if(pmsProductAttribute.getType()==1){
            pmsProductAttributeCategory.setParamCount(pmsProductAttributeCategory.getParamCount()+1);
        }
        int count = productAttributeCategoryMapper.updateById(pmsProductAttributeCategory);
        return count;
    }

    // TODO 修改商品属性
    @Override
    public int update(Long id, ProductAttributeParam productAttributeParam) {
        QueryWrapper<PmsProductAttribute> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        int update = productAttributeMapper.update(productAttributeParam, wrapper);
        return update;
    }

    // TODO 获取单个商品属性信息
    @Override
    public PmsProductAttribute getItem(Long id) {
        PmsProductAttribute productAttribute = productAttributeMapper.selectById(id);
        return productAttribute;
    }

    // TODO 删除多个商品属性值
    @Override
    public int delete(List<Long> ids) {
        int result = productAttributeMapper.deleteBatchIds(ids);
        return result;
    }

    // TODO 根据分类,获取商品属性信息
    @Override
    public List<PmsProductCategoryAttributeRelation> getProductAttrInfo(Long productCategoryId) {
        QueryWrapper<PmsProductCategoryAttributeRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("product_attribute_id",productCategoryId);
        List<PmsProductCategoryAttributeRelation> list = 
                pmsProductCategoryAttributeRelationMapper.selectList(wrapper);
        return list;
    }
}




